/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visual;

import Logica.Marca;
import Logica.Modelo;
import Logica.Segmento;
import Persistencia.exceptions.NonexistentEntityException;
import Persistencia.exceptions.PreexistingEntityException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JDesktopPane;

/**
 *
 * @author Alberto
 */
public class frmAltaModelo extends javax.swing.JInternalFrame {

    ControladoraVisual miVisual;
    JDesktopPane miEscritorio;
    DefaultComboBoxModel modeloModelos = new DefaultComboBoxModel();
    DefaultComboBoxModel modeloMarcas = new DefaultComboBoxModel();
    DefaultComboBoxModel modeloSegmentos = new DefaultComboBoxModel();
    Segmento unSegmento;
    Marca unaMarca;
    Modelo unModelo;
   
    public frmAltaModelo(ControladoraVisual miVisual, JDesktopPane miEscritorio) {
        initComponents();
        this.miVisual = miVisual;
        this.cmbMarcas.setModel(modeloMarcas);
        this.cmbSegmentos.setModel(modeloSegmentos);        
        this.cargarMarcas();
        this.cargarSegmentos();
       
        this.miEscritorio = miEscritorio;
        
    }
     public frmAltaModelo(ControladoraVisual miVisual, JDesktopPane miEscritorio, DefaultComboBoxModel modeloModelos) {
        initComponents();
        this.miVisual = miVisual;
        this.cmbMarcas.setModel(modeloMarcas);
        this.cmbSegmentos.setModel(modeloSegmentos);        
        this.cargarMarcas();
        this.cargarSegmentos();
        this.modeloModelos = modeloModelos;
        this.miEscritorio = miEscritorio;
        
    }
     List<Modelo> modelosMismaMarca;
    public void cargarMarcas(){
        List<Marca> marcas = this.miVisual.dameMarcas();
        for(Marca miMarca:marcas){
            this.cmbMarcas.addItem(miMarca);
        }
    }
    
    
    public void cargarSegmentos(){
   List<Segmento> segmentos = this.miVisual.dameSegmentos();
        for(Segmento miSegmento:segmentos){
            this.cmbSegmentos.addItem(miSegmento);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Salir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        btnAltaModelo = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        cmbMarcas = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cmbSegmentos = new javax.swing.JComboBox();
        btnAltaMarca = new javax.swing.JButton();
        bntAltaSegmento = new javax.swing.JButton();

        setTitle("Alta Modelo");

        Salir.setText("Salir");
        Salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SalirActionPerformed(evt);
            }
        });

        jLabel1.setText("Nombre");

        btnAltaModelo.setText("Alta Modelo");
        btnAltaModelo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaModeloActionPerformed(evt);
            }
        });

        jLabel2.setText("Marca");

        jLabel3.setText("Segmento");

        btnAltaMarca.setText("Nueva Marca");
        btnAltaMarca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaMarcaActionPerformed(evt);
            }
        });

        bntAltaSegmento.setText("Nuevo Segmento");
        bntAltaSegmento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntAltaSegmentoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAltaModelo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(Salir))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAltaMarca)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                        .addComponent(bntAltaSegmento))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addGap(7, 7, 7)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombre)
                            .addComponent(cmbMarcas, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmbSegmentos, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbMarcas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmbSegmentos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAltaMarca)
                    .addComponent(bntAltaSegmento))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Salir)
                    .addComponent(btnAltaModelo))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_SalirActionPerformed

    private void btnAltaModeloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaModeloActionPerformed
               
        String nombre = this.txtNombre.getText();  
        
        unSegmento = (Segmento) this.cmbSegmentos.getSelectedItem();
        unaMarca = (Marca) this.cmbMarcas.getSelectedItem();
        
        try {
           unModelo = miVisual.altaModelo(nombre, unaMarca, unSegmento);
           this.actualizarVentana(unModelo);
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(frmAltaModelo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(frmAltaModelo.class.getName()).log(Level.SEVERE, null, ex);
        }
            
     
         
    }//GEN-LAST:event_btnAltaModeloActionPerformed

 
    
    private void btnAltaMarcaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaMarcaActionPerformed
        frmAltaMarca miMarca = new frmAltaMarca(miVisual, this.modeloMarcas);
        miEscritorio.add(miMarca);
        miMarca.setMaximizable(true);
        miMarca.setClosable(true);
        miMarca.setVisible(true);
    }//GEN-LAST:event_btnAltaMarcaActionPerformed

    private void bntAltaSegmentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntAltaSegmentoActionPerformed
        frmAltaSegmento miSegmento = new frmAltaSegmento(miVisual, this.modeloSegmentos);
        miEscritorio.add(miSegmento);
        miSegmento.setMaximizable(true);
        miSegmento.setClosable(true);
        miSegmento.setVisible(true);
    }//GEN-LAST:event_bntAltaSegmentoActionPerformed
private void actualizarVentana(Modelo unModelo) {
        this.modeloModelos.addElement(unModelo);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Salir;
    private javax.swing.JButton bntAltaSegmento;
    private javax.swing.JButton btnAltaMarca;
    private javax.swing.JButton btnAltaModelo;
    private javax.swing.JComboBox cmbMarcas;
    private javax.swing.JComboBox cmbSegmentos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
